package com.aaa.inside.controller;


import com.aaa.entity.base.Locator;
import com.aaa.entity.inside.Puton;
import com.aaa.entity.stock.Stock;
import com.aaa.entity.system.User;
import com.aaa.inside.feign.BaseFeign;
import com.aaa.inside.feign.StockFeign;
import com.aaa.inside.feign.SystemFeign;
import com.aaa.inside.service.PutonService;
import com.aaa.inside.util.JwtUtils;
import com.aaa.vo.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author aaa
 * @since 2020-11-25
 */
@RestController
@RequestMapping("/inside/puton")
public class PutonController {
    @Autowired
    private PutonService putonService;
    @Autowired
    private BaseFeign baseFeign;
    @Autowired
    private StockFeign stockFeign;
    @Autowired
    private SystemFeign systemFeign;


    /*通过仓库id查找所有非入库区的库位*/
    @PostMapping("selectLocatorByWareId")
    @ApiOperation("通过仓库id查找所有非入库区的库位")
    public List<Map> selectLocatorByWareId(){
        Long warehouseid=41L;
        return putonService.selectLocatorByWareId(warehouseid);
    }
    /*通过仓库id查询所有库存中入库区的所有商品名称和编码*/
    @PostMapping("selectComByWareId")
    @ApiOperation("通过仓库id查询所有库存中入库区的所有商品名称和编码{去重}")
    public List<Map> selectComByWareId(){
        Long warehouseid=41L;
        return putonService.selectComByWareId(warehouseid);
    }

    /*通过商品编号和仓库id查询该仓库在入库区的源货位*/
    @PostMapping("selectOriLocByCombar/{commoditybar}")
    @ApiOperation("通过商品编号和仓库id查询该仓库在入库区的源货位")
    public List<Map> selectOriLocByCombar(@PathVariable String commoditybar){
        Long warehouseid=41L;
        return putonService.selectOriLocByCombar(commoditybar,warehouseid);
    }
    /*上架操作*/
    @PostMapping("puton")
    @ApiOperation("上架操作")
    public R addPuton(@RequestBody Puton puton, HttpServletRequest request){
        if (puton.getQty()==0){
            return R.error().message("数量不能为0");
        }

        String token = request.getHeader("token");
        Long userid = JwtUtils.getMemberIdByJwtToken(token);
        User user = systemFeign.selectUserById(userid);

        Long warehouseid=41L;
        /*查出库存中可上架的一个商品*/
        Stock stock=stockFeign.selectStockByComBarOriLo(puton.getCommoditybar(),puton.getOldlocatorcode(),warehouseid);
        /*通过商品编码查出商品名称和id*/
        Map comList=selectCommoditynameByBar(puton.getCommoditybar());
        Long commodityid= (Long) comList.get("id");
        String commodityname= (String) comList.get("name");
        /*通过源货位编码查出源货位id和名称*/
        Map OldLoIdName=selectOriLoIdNameByOldLoCode(puton.getOldlocatorcode());
        Long oldlocatorid= (Long) OldLoIdName.get("id");
        String oldlocatorname= (String) OldLoIdName.get("name");
        /*通过目标货位编码查出目标货位id和名称状态和类型整个实体*/
        Locator locator= baseFeign.selectTarLocByLoCode(puton.getLocatorcode(),warehouseid);
        if(puton.getQty()<=stock.getUqty()){
            QueryWrapper<Puton> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("commoditybar",puton.getCommoditybar());
            queryWrapper.eq("oldlocatorcode",puton.getOldlocatorcode());
            queryWrapper.eq("locatorcode",puton.getLocatorcode());
            queryWrapper.eq("state","1");
            Puton puton2 = putonService.getOne(queryWrapper);
            if (puton2!=null){
                puton2.setQty(puton2.getQty()+puton.getQty());
                putonService.updateById(puton2);
                stock.setUqty(stock.getUqty()-puton.getQty());
                stock.setLqty(stock.getLqty()+puton.getQty());
                stockFeign.updateById(stock);
                return R.ok().message("上架成功");
            }
            Puton puton1=new Puton();
            puton1.setCommoditybar(puton.getCommoditybar());
            puton1.setCommodityname(commodityname);
            puton1.setCommodityid(commodityid);
            puton1.setLocatorid(locator.getId());
            puton1.setLocatorcode(locator.getCode());
            puton1.setLocatorname(locator.getName());
            puton1.setOldlocatorid(oldlocatorid);
            puton1.setOldlocatorcode(puton.getOldlocatorcode());
            puton1.setOldlocatorname(oldlocatorname);
            puton1.setQty(puton.getQty());
            puton1.setWarehouseid(warehouseid);
            puton1.setCreateid(user.getUserId());
            puton1.setCreator(user.getName());
            Date now=new Date();
            puton1.setCreatetime(now);
            putonService.save(puton1);
            stock.setUqty(stock.getUqty()-puton.getQty());
            stock.setLqty(stock.getLqty()+puton.getQty());
            stockFeign.updateById(stock);
            return R.ok().message("上架成功");
        }else {
            return R.error().message("该库位商品数量不足，请核实！");
        }
    }
    /*上架管理审核操作*/
    @PostMapping("putonCheck")
    @ApiOperation("上架管理审核操作")
    public R putonCheck(@RequestBody Puton puton,HttpServletRequest request){
        String token = request.getHeader("token");
        Long userid = JwtUtils.getMemberIdByJwtToken(token);
        User user = systemFeign.selectUserById(userid);

        Long warehouseid=41L;
        if(puton.getState().equals("1")) {
            /*库存中对应的源库位商品数量更新*/
                Stock stock = stockFeign.selectStockByComBarOriLo(puton.getCommoditybar(), puton.getOldlocatorcode(),warehouseid);


            stock.setCqty(stock.getCqty()-puton.getQty());
            stock.setLqty(stock.getLqty()-puton.getQty());
            stockFeign.updateById(stock);
            if (stock.getCqty()==0){
                stockFeign.removeById(stock.getId());
            }

            Stock stock1 = new Stock();
            stock1.setLocatorid(puton.getLocatorid());
            stock1.setLocatorcode(puton.getLocatorcode());
            stock1.setLocatorname(puton.getLocatorname());
            stock1.setCommodityid(puton.getCommodityid());
            stock1.setCommoditybar(puton.getCommoditybar());
            stock1.setCommodityname(puton.getCommodityname());
            stock1.setWarehouseid(warehouseid);
            /*判断库存有没有同货位同商品的数据有更新没有插入*/
            Stock stock2 = stockFeign.selectStockByComBarOriLo(puton.getCommoditybar(), puton.getLocatorcode(),warehouseid);
            if (stock2 == null) {
                stock1.setLqty(0);
                stock1.setCqty(puton.getQty());
                stock1.setUqty(puton.getQty());
                stockFeign.save(stock1);
            } else {
                stock2.setCqty(stock2.getCqty()+puton.getQty());
                stock2.setUqty(stock2.getUqty()+puton.getQty());
                Map map=new HashMap();
                map.put("locatorcode", puton.getLocatorcode());
                map.put("commoditybar", puton.getCommoditybar());
                map.put("warehouseid",warehouseid);
                stockFeign.update(stock2, map);
            }

            /*审核状态变成已审核*/
            puton.setState("2");
            puton.setAuditor(user.getName());
            Date now=new Date();
            puton.setAudittime(now);
            putonService.updateById(puton);
            return R.ok().message("审核成功");
        }else {
            return R.error().message("已审核请勿重复操作");
        }

    }
    /*上架撤销审核操作*/
    @PostMapping("putonCancelCheck")
    @ApiOperation("上架管理撤销审核操作")
    public R putonCancelCheck(@RequestBody Puton puton) {
        Long warehouseid=41L;
        if (puton.getState().equals("1")){
            return  R.error().message("此订单尚未审核，请先审核！");
        }

        Stock stock = stockFeign.selectStockByComBarOriLo(puton.getCommoditybar(), puton.getOldlocatorcode(),warehouseid);
        if (stock==null){
            Stock stock1=new Stock();
            stock1.setCqty(puton.getQty());
            stock1.setUqty(0);
            stock1.setLqty(puton.getQty());
            stock1.setLocatorid(puton.getOldlocatorid());
            stock1.setLocatorcode(puton.getOldlocatorcode());
            stock1.setLocatorname(puton.getOldlocatorname());
            stock1.setCommodityid(puton.getCommodityid());
            stock1.setCommoditybar(puton.getCommoditybar());
            stock1.setCommodityname(puton.getCommodityname());
            stockFeign.save(stock1);
        }else {
            stock.setCqty(stock.getUqty() + puton.getQty());
            stock.setLqty(puton.getQty());
            stockFeign.updateById(stock);
        }


        Stock stock2 = stockFeign.selectStockByComBarOriLo(puton.getCommoditybar(), puton.getLocatorcode(),warehouseid);
        if(stock2.getCqty()==puton.getQty()){
            stockFeign.removeById(stock2.getId());
        }else {
            stock2.setUqty(stock2.getUqty()-puton.getQty());
            stock2.setCqty(stock2.getCqty()-puton.getQty());
            stockFeign.updateById(stock2);
        }
        /*审核状态变成未审核*/
        puton.setState("1");
        putonService.updateById(puton);
        return R.ok().message("撤销审核成功");
    }

    /*通过商品编码查出商品名称和id*/
    private Map selectCommoditynameByBar(String commoditybar) {
        return putonService.selectCommoditynameByBar(commoditybar);
    }
    /*通过源货位编码查出源货位id和名称*/
    private Map selectOriLoIdNameByOldLoCode(String oldlocatorcode) {
        return putonService.selectOriLoIdNameByOldLoCode(oldlocatorcode);
    }
    /*分页按条件查询所有上架管理*/
    @PostMapping("searchPuton/{page}/{limit}")
    @ApiOperation("分页按条件查询所有上架管理")
    public R searchPuton(@PathVariable Integer page, @PathVariable Integer limit, @RequestBody Puton puton){
        return putonService.searchPuton(page,limit,puton);
    }
    /*删除上架管理*/
    @PostMapping("deletePuton")
    public R deletePuton(@RequestBody Puton puton){
        boolean b = putonService.removeById(puton.getId());
        if(b){
            Stock stock=stockFeign.selectStockByComBarOriLo(puton.getCommoditybar(),puton.getOldlocatorcode(),41L);
            stock.setLqty(stock.getLqty()-puton.getQty());
            stock.setUqty(stock.getUqty()+puton.getQty());
            Boolean a = stockFeign.updateById(stock);
            if (a){
                return R.ok().message("删除成功");
            }else {
                return R.error().message("删除失败");
            }
        }else {
            return R.error().message("删除失败");
        }

    }

}

